package com.hand.db;

import com.hand.ui.MainWindowsComponent;
import com.hand.utils.InfoBuild;
import javafx.scene.control.TextArea;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

public class SqlUtil {
    public static boolean insert(Statement st, String[] valuse) throws SQLException, InterruptedException {
        try {
            String sql = "INSERT INTO CMF_CLM_MGR_FLEXFIELDS ( MGR_FLEXFIELD_ID, CLAIM_HEADER_ID, TABLE_KEY_ID, " +
                    "TABLE_NAME, OBJECT_VERSION_NUMBER, CREATION_DATE, CREATED_BY, LAST_UPDATE_DATE, LAST_UPDATED_BY, " +
                    "ATTR_ZD_TYPE_PRODUCT, ATTR_ZD_REVENUE_LINE ) SELECT " +
                    "CMF_CLM_MGR_FLEXFIELDS_S.nextval MGR_FLEXFIELD_ID," +
                    "npcexp.CLAIM_HEADER_ID CLAIM_HEADER_ID," +
                    "npcexp.CLAIM_EXPENSE_LINE_ID TABLE_KEY_ID," +
                    "'CMF_CLM_CLAIM_NPCEXP_LINES' TABLE_NAME," +
                    "1 OBJECT_VERSION_NUMBER," +
                    "SYSDATE CREATION_DATE," +
                    "-1 CREATED_BY," +
                    "SYSDATE LAST_UPDATE_DATE," +
                    "-1 LAST_UPDATED_BY," +
                    valuse[1] +" ATTR_ZD_TYPE_PRODUCT," +
                    valuse[2] +" ATTR_ZD_REVENUE_LINE" +
                    " FROM CMF_CLM_CLAIM_NPCEXP_LINES npcexp" +
                    " WHERE npcexp.ATTRIBUTE15 = '" + valuse[0] +"'";

            System.out.println(sql);
            TextArea infoArea = MainWindowsComponent.getInstance().getInfoArea();
            String info = InfoBuild.build(infoArea.getText(), "执行SQL：" + sql);
            infoArea.setText(info);
            int row = st.executeUpdate(sql);
            if (row == 0) {
                return false;
            }
            return true;

        } catch (Exception e) {
            TextArea infoArea = MainWindowsComponent.getInstance().getInfoArea();
            String info = InfoBuild.build(infoArea.getText(), "SQl运行错误：" + e.getMessage());
            infoArea.setText(info);
            throw e;
        }
    }

    public void query(Connection conn, String tableName, String[] columnLabels) {
        Statement st;
        ResultSet rs;
        try {
            st=conn.createStatement();
            StringBuilder sql = new StringBuilder();
            sql.append("select ");
            for (String columnLabel : columnLabels) {
                sql.append(columnLabel).append(",");
            }
            sql.deleteCharAt(sql.length() - 1);
            sql.append(" from ").append(tableName);
            rs=st.executeQuery(sql.toString());
            while(rs.next()) {
                for (String columnLabel : columnLabels) {
                    System.out.println(rs.getString(columnLabel));
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }finally {
            try {
                assert conn != null;
                conn.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
    }
}
